/**
 * @author		FengChaoHua
 * @create		2018年4月16日 下午6:43:11
 * @project	weixin_guide
 * @desc		
 */
package top.wardyan.base.service.impl;


import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import top.wardyan.base.service.IService;

/**
 * @author FengChaoHua
 *
 */
public abstract class ServiceImpl<T extends Model<T>> implements IService<T> {
	
	//protected Log log = Log.getLog(ServiceImpl.class);
	
	protected Log log = Log.getLog(this.getClass());
	
	protected T dao;
	
	protected String sql;	// = "select * from " + getTableName() + "";;
	
	//protected abstract String getTableName();
	
	//protected abstract T this.dao;
	
	//protected abstract String getPrimaryKeyField();

	protected String table;
	protected String primaryKey;
	
	protected String genSql() {
		return "SELECT * FROM " + this.table + "";
	}
	
	public ServiceImpl(T dao, String table, String primaryKey) {
		this.dao = dao;
		this.table = table;
		this.primaryKey = primaryKey;
		this.sql = this.genSql();
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#save(java.lang.Object)
	 */
	@Override
	public Boolean save(T object) {
		return object.save();
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#update(java.lang.Object)
	 */
	@Override
	public Boolean update(T object) {
		return object.update();
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#delete(java.lang.Object)
	 */
	@Override
	public Boolean delete(T object) {
		return object.delete();
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#findBySID(java.lang.Integer)
	 */
	@Override
	public T findByID(Integer id) {
		return id == null ? null : this.dao.findById(id);
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#findBySql(java.lang.String)
	 */
	@Override
	public T findBySql(String sql, Object... params) {
		if (sql == null || sql.length() == 0){
			return null;
		}else{
			List<T> temp = this.dao.find(sql, params);
			return (temp != null && temp.size() > 0) ? temp.get(0) : null;
		}
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#list()
	 */
	@Override
	public java.util.List<T> list() {
		return this.dao.find(this.sql);
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#list(java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public Page<T> list(Integer page, Integer pageSize) {
		//return this.list(this.select, this.sqlExceptSelect, page, pageSize);
		return this.list(this.sql, page, pageSize, null);
	}
	
	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#list(java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.Object[])
	 */
	@Override
	public Page<T> list(String sql, Integer page, Integer pageSize, String orderBy, Object... params) {
		String str = sql + "";
		Integer firstResult = (page - 1) * pageSize;
		Integer totalRow = this.count(str, params);
		Integer totalPage =totalRow / pageSize + ((totalRow % pageSize == 0) ? 0 : 1 );
		Object[] temps = new Object[params.length*2];
		for(Integer i = 0,size = temps.length; i < size; i++){
			temps[i] = params[i >= params.length ? i - params.length : i];
		}
		
		str = "select top "+ pageSize + sql.substring(sql.indexOf(" ")) + "" ;
		//if (params != null && params.length > 0){
		if (str.indexOf(" where ") > 0){
			str += " and ";
		}else{
			str += " where ";
		}
		
		str += this.primaryKey + " not in (select top " + firstResult +" " + this.primaryKey + " " + sql.substring(sql.indexOf("*")+1)  + ")";
		
		if (orderBy != null && orderBy.length() > 0){
			str += " order by " + orderBy;
		}
		
		str = str.replace("  ", " ");
		//str = "select top " + pageSize + " from (" + str + ")";
		
		//log.info("serviceImpl list str: " + str + ", params:" + temps.toString());
		return new Page<T>(this.dao.find(str, temps), page, pageSize, totalPage, totalRow);
		
		
		
		
	}

	
	

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#List(java.lang.String, java.lang.Object[])
	 */
	@Override
	public java.util.List<T> List(String sql, Object... params) {
		return this.dao.find(sql, params);
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#count()
	 */
	@Override
	public Integer count() {
		//List<T> temp = this.dao.find("select count(*) as count " + this.sqlExceptSelect);
		List<T> temp = this.dao.find(this.sql.replace("*", " count(*) as count ") );
		return (temp != null && temp.size() > 0) ? temp.get(0).getInt("count") : 0;
	}

	/* (non-Javadoc)
	 * @see com.uuerp.service.IService#count(java.lang.String, java.lang.Object[])
	 */
	@Override
	public Integer count(String sql, Object... params) {
		List<T> temp = this.dao.find(sql, params);
		return (temp != null && temp.size() > 0) ? temp.size() : 0;
	}

	
	
}
